// @flow
import React from 'react'
import styles from './CustomerFillter.css'
import universalFetch, { handleFetchError } from 'store/modules/fetch'
import { Input, Row, Col, Button, Select } from 'antd'

const Option = Select.Option

type Props = {
  getData: Function
}

type State = {
  instury: Array<Object>,
  location: Array<Object>
}

export default class AddChartEvent extends React.Component {
  props: Props
  state: State
  searchQuery: string
  searchInfo: Object
  handleCustomerName: Function
  handleSearch: Function
  handleIndustry: Function
  handleLocation: Function
  constructor (props: Object) {
    super(props)
    this.state = {
      instury: [{ id: 0, value: '全部' }],
      location: [{ id: 0, value: '全部' }]
    }
    this.handleCustomerName = this.handleCustomerName.bind(this)
    this.handleSearch = this.handleSearch.bind(this)
    this.handleIndustry = this.handleIndustry.bind(this)
    this.handleLocation = this.handleLocation.bind(this)
    this.handleSearch = this.handleSearch.bind(this)
    this.searchInfo = {}
  }

  componentDidMount () {
    // this.getFillterData()
    this.getFillterDatas(`${__SERVICE_API__}sys/dicts?type=industry`, 'instury')
    this.getFillterDatas(`${__SERVICE_API__}sys/dicts?type=location`, 'location')
  }
  getFillterDatas (url: string, name: string) {
    universalFetch(url)
    .then(res => res.json())
    .then((json) => {
      if (json.code !== 200000) {
        throw new Error(JSON.stringify({ code: json.code, message: json.message }))
      }
      let data = this.state[name]
      data.push(...json.result)
      this.setState({ [name]: data })
    })
    .catch((error) => {
      handleFetchError(error)
      console.error(error)
    })
  }
  handleSearch () {
    let { customerName, instury, location } = this.searchInfo
    //  不确定是否有值,没有则设置为空
    customerName = customerName || ''
    instury = instury || ''
    location = location || ''
    let searchQuery = `&name=${customerName}&industry=${instury}&location=${location}`
    this.props.getData(searchQuery)
  }
  handleCustomerName (e: any) {
    this.searchInfo.customerName = e.target.value
  }
  handleIndustry (value: any) {
    if (value === '全部') {
      this.searchInfo.instury = ''
      return
    }
    this.searchInfo.instury = value
  }
  handleLocation (value: any) {
    if (value === '全部') {
      this.searchInfo.location = ''
      return
    }
    this.searchInfo.location = value
  }

  render () {
    const { instury, location } = this.state
    return (
      <div className={styles['searchBox']}>
        <Row gutter={24} justify='center'>
          <Col className={styles['ant-col-3']} span={3}>
            <div className='gutter-box'>
              <Input placeholder='客户名称' onInput={this.handleCustomerName} onPressEnter={this.handleSearch} />
            </div>
          </Col>
          <Col className={styles['ant-col-2']} span={2}>
            <div className='gutter-box'>
              <Select
                showSearch
                placeholder='所属行业'
                optionFilterProp='children'
                filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
                className={styles['selctBox']}
                onSelect={this.handleIndustry}
             >
                {instury.map((item, index) => {
                  return <Option value={item.value} key={index} className={styles['optionBox']}>{item.value}</Option>
                })}
              </Select>
            </div>
          </Col>
          <Col className={styles['ant-col-2']} span={2}>
            <div className='gutter-box'>
              <Select
                showSearch
                placeholder='所属地区'
                optionFilterProp='children'
                filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
                className={styles['selctBox']}
                onSelect={this.handleLocation}
             >
                {location.map((item, index) => {
                  return <Option value={item.value} key={index} className={styles['optionBox']}>{item.value}</Option>
                })}
              </Select>
            </div>
          </Col>
          <Button icon='search' onClick={this.handleSearch} />
        </Row>
      </div>
    )
  }
}
